package com.example.tavern.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@Controller
public class CommonController {

    @ResponseBody
    @RequestMapping("/upload")
    public Map upload(MultipartFile file) throws IOException {
        System.out.println(file.getSize());
        Map map = new HashMap<String,Object>();
        if (!file.isEmpty()){
            String oldName = file.getOriginalFilename();
            String newName = UUID.randomUUID().toString()
                    + oldName.substring(oldName.lastIndexOf("."), oldName.length());
            file.transferTo(new File("D:\\头条视频\\小程序源码\\10期酒店预订\\hotel-main\\tavern\\src\\main\\resources\\static\\image\\"+newName));
            map.put("code","0");
            map.put("msg","上传成功");
            map.put("name",newName);
            return map;
        }else{
            return map;
        }
    }
}

